import java.util.*;

/**
 *
 */
public class BinaryTree {
    static class TreeNode {
        public char val;
        public int val1;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    public TreeNode root;
    public int preindex;

    public TreeNode Creat() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode P = new TreeNode('P');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        D.right = P;
        return A;
    }
    public TreeNode Creat1() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');

        A.left = B;
        A.right = C;

        return A;
    }
    public TreeNode Creat2() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');

        A.left = B;
        B.right = C;

        return A;
    }
    public TreeNode Creat3() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('A');
        TreeNode C = new TreeNode('A');
        TreeNode D = new TreeNode('A');
        TreeNode E = new TreeNode('B');

        A.right = B;
        B.right = C;
        C.right = D;
        D.left = E;
        return A;
    }
    public TreeNode Creat4() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('A');
        TreeNode C = new TreeNode('B');


        A.right = B;
        B.left = C;
        return A;
    }
    public void preOredr(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOredr(root.left);
        preOredr(root.right);
    }
    public void preOredrNor(TreeNode root){
        Stack<TreeNode>stack =new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.empty()){
            while(cur!=null){
                stack.add(cur);
                System.out.print(cur.val+" ");
                cur=cur.left;
            }
            TreeNode tmp=stack.pop();
            cur=tmp.right;
        }
    }

//前序遍历放在叔祖里面
    public List<Character> preOredr1(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        } else {
            ret.add(root.val);
            List<Character> leftTree = preOredr1(root.left);
            ret.addAll(leftTree);
            List<Character> rightTree = preOredr1(root.right);
            ret.addAll(rightTree);
            return ret;
        }
    }

    public void inOredr(TreeNode root) {
        if (root == null) {
            return;
        }

        inOredr(root.left);
        System.out.print(root.val + " ");
        inOredr(root.right);
    }
    public void inOredrNor(TreeNode root){
        Stack<TreeNode>stack =new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.empty()){
            while(cur!=null){
                stack.add(cur);
                cur=cur.left;
            }
            TreeNode tmp=stack.pop();
            System.out.print(tmp.val+" ");
            cur=tmp.right;
        }
    }

    public void postOredr(TreeNode root) {
        if (root == null) {
            return;
        }

        postOredr(root.left);
        postOredr(root.right);
        System.out.print(root.val + " ");

    }
    public void postOredrNor(TreeNode root) {
        if(root==null){
            return ;
        }
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode prev=null;
        stack.add(cur);
        while(cur!=null||!stack.empty()){
            while(cur!=null){
                stack.add(cur);
                cur=cur.left;
            }
            TreeNode top=stack.peek();
            if(top.right==null||top.right==prev){
                stack.pop();
                System.out.print(top.val+" ");
                prev=top;
            }else{
                cur=top.right;
            }
        }

    }

    public List<TreeNode> preOredr2(TreeNode root) {
        List<TreeNode> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }
        ret.add(root);
        List<TreeNode> leftTree = preOredr2(root.left);
        ret.addAll(leftTree);
        List<TreeNode> rightTree = preOredr2(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    public List<TreeNode> inOredr2(TreeNode root) {
        List<TreeNode> ret = new ArrayList<>();
        if (root == null) {
            return ret;
        }

        List<TreeNode> leftTree = inOredr2(root.left);
        ret.addAll(leftTree);
        ret.add(root);
        List<TreeNode> rightTree = inOredr2(root.right);
        ret.addAll(rightTree);
        return ret;
    }

    //遍历的思路
    int size = 0;

    public void size(TreeNode root) {
        if (root == null) {
            return;
        }
        size++;
        size(root.left);
        size(root.right);
    }

    public int getLeaves(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeaves(root.left) + getLeaves(root.right);
    }

    public int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left, k - 1)
                + getKLevelNodeCount(root.right, k - 1);
    }
    //左/右+1
    public int getHeight(TreeNode root){
        if(root==null){
            return 0;
        }
        int left=getHeight(root.left);
        int right=getHeight(root.right);
        return left>right?left+1:right+1;
    }
    public Boolean find(TreeNode root,char key){
        if(root==null){
            return false;
        }
        if(root.val==key){
            return true;
        }else{
            boolean left=find(root.left,key);
            if(left==true){
                return true;
            }
            boolean right=find(root.right,key);
            if(right==true){
                return true;
            }
        }

        return  false;
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(q==null&&p==null){
            return true;
        }
        if(q==null||p==null){
            return false;
        }
        if(q.val!=p.val){
            return false;
        }
        return isSameTree(p.left,q.left)
                &&isSameTree(p.right,q.right);
    }
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(isSameTree(root,subRoot)){
            return true;
        }else{
            //这个要求太强了 意思是每个左右树至少有一个跟sub树完全一样
            //return isSameTree(root.left,subRoot)||isSameTree(root.right,subRoot);
            return isSubtree(root.left,subRoot)||isSubtree(root.right,subRoot);
        }
    }
    public static int i=0;
    public static TreeNode CreatTree(String s){
        TreeNode root=null;
        if(s.charAt(i)=='#'){
            i++;
        }else{
                root=new TreeNode(s.charAt(i));
                i++;
                root.left=CreatTree(s);
                root.right=CreatTree(s);
            }
        return root;
    }
    public static void inOrder(TreeNode root){
        if(root==null){
            return ;
        }
        inOrder(root.left);
        System.out.print(root.val+" ");
        inOrder(root.right);
    }
    public  static void levelOrder(TreeNode root){
        if(root==null){
            return ;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left != null){
                queue.add(cur.left);
            }
            if(cur.right != null){
                queue.add(cur.right);
            }
        }

    }
    public static List<List<TreeNode>> levelOrder_k(TreeNode root) {
        List<List<TreeNode>> ret= new ArrayList<>();
        if(root==null){
            return ret;
        }
        Queue<TreeNode> queue= new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            int size=queue.size();
            List<TreeNode> tmp=new ArrayList<>();
            while(size>0){
                TreeNode cur =queue.poll();
                tmp.add(cur);
                size--;
                if(cur.left!=null){
                    queue.add(cur.left);
                }
                if(cur.right!=null){
                    queue.add(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
    public static List<List<TreeNode>> levelOrder_k1(TreeNode root){
        List<List<TreeNode>> ret= new ArrayList<>();
        Queue<TreeNode>queue =new LinkedList<>();
        TreeNode cur=null;
        queue.add(root);
        while(!queue.isEmpty()){

            int se=queue.size();
            cur=queue.poll();
            ArrayList<TreeNode>tmp=new ArrayList<>();
            while(se>0){
                tmp.add(cur);
                if(cur.left!=null){
                    queue.add(cur.left);
                }
                if(cur.right!=null){
                    queue.add(cur.right);
                }
                se--;
            }
            ret.add(tmp);
        }
        return ret;
    }

    public static boolean isCompleteTree(TreeNode root){
        if(root==null){
            return true;
        }

        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if (cur != null) {

                queue.add(cur.left);
                queue.add(cur.right);
            }else{
                break;
            }
        }

        while(!queue.isEmpty()){
            if(queue.poll()!=null){
                return false;
            }
        }
        return true;
    }
    public static List<List<TreeNode>> levelOrder_k11(TreeNode root){
      List<List<TreeNode>> ret= new ArrayList<>();
        Queue<TreeNode>  queue=new LinkedList<>();
        if(root==null){
            return ret;
        }
        queue.add(root);
        while(!queue.isEmpty()){
            List<TreeNode> tmp=new ArrayList<>();
            int size= queue.size();
            while(size>0){
                TreeNode cur=queue.poll();
                tmp.add(cur);
                size--;
                if(cur.left!=null){
                    queue.add(cur.left);
                }
                if(cur.right!=null){
                    queue.add(cur.right);
                }
            }
            ret.add(tmp);
        }
        return ret;
    }
    boolean isCompleteTree1(TreeNode root){
        if(root==null){
            return true;
        }
        Queue<TreeNode>queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            TreeNode cur=queue.poll();
            if(cur!=null){
                queue.add(cur.left);
                queue.add(cur.right);
            }
            else{
                break;
            }
        }
        while(!queue.isEmpty()){
            if(queue.poll()!=null){
                return false;
            }
        }
        return true;
    }
  /*  public TreeNode lowestCommonAncestor_(TreeNode root, TreeNode p, TreeNode q){
        if(root==null){
            return null;
        }
        if(root==p||root==q){
            return root;
        }
        TreeNode leftTree=lowestCommonAncestor_(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor_(root.right,p,q);
        if(leftTree!=null&&rightTree!=null){
            return root;
        }else{
            if(leftTree!=null){
                return leftTree;
            }
            return rightTree;
        }
    }*/
    public   boolean getPath(TreeNode root,TreeNode dest,Stack<TreeNode> stack ) {
        if(root==null){
            return false;
        }
        if(root==dest){
            stack.add(root);
            return true;
        }
        stack.add(root);
        boolean flg1=getPath(root.left,dest,stack);
        if(flg1){
            return true;
        }
        boolean flg2=getPath(root.right,dest,stack);
        if(flg2){
            return true;
        }
        stack.pop();
        return false;


    }
  public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
      Stack<TreeNode>stackP=new Stack<>();
      Stack<TreeNode>stackQ=new Stack<>();
      getPath(root,p,stackP);
      getPath(root,q,stackQ);
      int sizeP=stackP.size();
      int sizeQ=stackQ.size();
      if(sizeP-sizeQ>0){
          int gap=sizeP-sizeQ;
          while(gap>0){
              stackP.pop();
              gap--;
          }
      }else{
          int gap=sizeQ-sizeP;
          while(gap>0){
              stackQ.pop();
              gap--;
          }
      }
      while(!stackP.empty()&&!stackQ.empty()){
          if(stackP.peek().equals(stackQ.peek())){
              return stackP.peek();
          }
          stackP.pop();
          stackQ.pop();
      }
      return null;

  }

 /*   public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){
        if(root==null){
            return null;
        }
        if(p==root||q==root){
            return root;
        }
        TreeNode leftTree=lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree=lowestCommonAncestor(root.right,p,q);
        if(leftTree!=null&&rightTree!=null){
            return root;
        }else if (leftTree!=null)
            {
                return leftTree;
            }
            else{
                return rightTree;
            }

        }*/
  /*  private boolean getPath(TreeNode root,TreeNode node,Stack<TreeNode> stack){
        if(root==null){
            return false;
        }
        stack.add(root);
        if(root==node){
            return true;
        }
        boolean flg1=getPath(root.left,node,stack);
        if(flg1){
            return true;
        }
        boolean flg2=getPath(root.right,node,stack);
        if(flg2){
            return true;
        }
        stack.pop();
        return false;


    }*/
    public TreeNode lowestCommonAncestor1(TreeNode root, TreeNode p, TreeNode q){
        Stack<TreeNode> stackP=new Stack<>();
        Stack<TreeNode> stackQ=new Stack<>();
        getPath(root,p,stackP);
        getPath(root,q,stackQ);

        int size=0;
        int sizeP=stackP.size();
        int sizeQ=stackQ.size();
        if(sizeP-sizeQ>0){
            size=sizeP-sizeQ;
            while(size!=0){
                stackP.pop();
                size--;
            }
        }else{
            size=sizeQ-sizeP;
            while(size!=0){
                stackQ.pop();
                size--;
            }
        }
        while(!stackP.empty()&&!stackQ.empty()){
            if(stackP.peek().equals(stackQ.peek())){
                return stackP.peek();
            }
            stackQ.pop();
            stackP.pop();
        }
        return null;
    }
    public TreeNode buildTree(char[] preorder, char[] inorder) {
        return buildChilidTree(preorder,inorder,0,inorder.length-1);
    }
    public   TreeNode buildChilidTree(char[] preorder,char[] inorder,int inbegin,int inend){
        if(inbegin>inend){
            return null;
        }
        TreeNode root =new TreeNode(preorder[preindex]);
        int rindex=findIndex(inorder,inbegin,inend,preorder[preindex]);
        if(rindex==-1){
            return null;
        }
        preindex++;
        root.left= buildChilidTree(preorder,inorder,inbegin,rindex-1);
        root.right=buildChilidTree(preorder,inorder,rindex+1,inend);
        return root;


    }
    private int findIndex(char[]inorder,int inbegin,int inend,char key){
        for (int i = 0; i <= inend; i++) {
            if(inorder[i]==key){
                return i;
            }
        }
        return -1;
    }
    private void tree2strChild(TreeNode t,StringBuilder stringBuilder){
        if(t==null){
            return ;
        }
        stringBuilder.append(t.val);

        if(t.left!=null){
            stringBuilder.append("(");
            tree2strChild(t.left,stringBuilder);
            stringBuilder.append(")");
        }else{
            if(t.right==null){
                return ;
            }else{
                stringBuilder.append("()");
            }
        }
        if(t.right!=null){
            stringBuilder.append("(");
            tree2strChild(t.right,stringBuilder);
            stringBuilder.append(")");
        }else{
            return ;
        }

    }
    public String tree2str(TreeNode root) {
        StringBuilder stringBuilder=new StringBuilder();
        tree2strChild(root,stringBuilder);
        return stringBuilder.toString();
    }
}
